package com.yuan.springcloud.product.controller;

/**
 * ProductFormBean对象的请求JSON
 * data = {
 *     'productName': '仙剑奇侠传七',
 *     'price': 200.00,
 *     'productDesc': '仙剑奇侠传最新续作！仗剑江湖梦已远，仙剑奇侠永流传！',
 *     'caption': '虚幻四引擎打造全新的仙剑世界~',
 *     'inventory': 6000000,
 *     'malls': ['天猫', '淘宝', '京东'],
 *     'historyProducts': ['仙剑一', '仙剑四', '仙剑六'],
 *     'productDetail': {
 *         'detail': '<div><a href="http://xj.com/new?version=7">仙剑奇侠传七</a></div>'
 *     },
 *     'productSkuList': [
 *         {
 *             'skuName': '标准版',
 *             'skuPrice': 99.00,
 *             'skuPropertyList': [
 *                 {
 *                     'skuLabel': '版本',
 *                     'skuValue': '标准版',
 *                 },
 *                 {
 *                     'skuLabel': '收货方式',
 *                     'skuValue': '快递',
 *                 }
 *             ]
 *         },
 *         {
 *             'skuName': '豪华版',
 *             'skuPrice': 599.00,
 *             'skuPropertyList': [
 *                 {
 *                     'skuLabel': '版本',
 *                     'skuValue': '豪华版',
 *                 },
 *                 {
 *                     'skuLabel': '收货方式',
 *                     'skuValue': '快递',
 *                 }
 *             ]
 *         },
 *     ]
 * }
 */

import com.yuan.springcloud.product.bean.ProductForm;
import com.yuan.springcloud.product.bean.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author YouYuan
 * @date 2020/4/7 4:31
 */
@RestController
@RequestMapping("/test")
public class TestController {

    /**
     * 使用@RequestBody注解可以将json自动转换成Bean，并且支持复杂类型嵌套的转换
     * @param request
     * @param product
     * @return
     */
    @RequestMapping(value = "/requestBody", method = {RequestMethod.GET, RequestMethod.POST})
    public Map requestBody(HttpServletRequest request, @RequestBody ProductForm product){
        Map<String, String[]> parameterMap = request.getParameterMap();
        System.out.println(parameterMap);
        System.out.println(product);
        return Result.success(product);
    }

    /**
     * 没有加@RequestBody只能自动转换简单对象，不支持对象嵌套等复杂对象的转换
     * @param request
     * @param product
     * @return
     */
    @RequestMapping(value = "/request", method = {RequestMethod.GET, RequestMethod.POST})
    public Map request(HttpServletRequest request, ProductForm product){
        Map<String, String[]> parameterMap = request.getParameterMap();
        parameterMap.forEach((k,v) -> {
            for (int i = 0; i < v.length; i++) {
                System.out.println(k + " --" + i + "-- " + v[i]);
            }
        });
        System.out.println(product);
        return Result.success(product);
    }
}
